package org.study.shell;

import java.util.ArrayList;
import java.util.List;

public class ShellNetWork {

    /**
     * 虚拟机中，修改ip地址为静态
     * @param ip ip地址
     * @param mask 子网掩码
     * @param gateway 网关地址
     * @param dns DNS服务器
     */
    public static List<String> netStatic(String ip,String mask,String gateway,String dns){
        List<String> commands = new ArrayList<String>();
        commands.addAll(ShellFile.findByName("/etc/sysconfig/network-scripts/","ifcfg-en","if_in"));
        commands.add(ShellFile.replaceContent("'s/BOOTPROTO=dhcp/BOOTPROTO=static/g","$if_in"));
        commands.add("echo IPADDR="+ip+" >> $if_in");
        commands.add("echo NETMASK="+mask+" >> $if_in");
        commands.add("echo GATEWAY="+gateway+" >> $if_in");
        commands.add("echo DNS1="+dns+" >> $if_in");
        commands.add("echo DNS2=8.8.8.8 >> $if_in");
        return commands;
    }

    /**
     * 修改静态ip地址，需要
     * @param oldIp  旧ip地址
     * @param newIp  新ip地址
     */
    public static List<String> changeIp(String oldIp, String newIp){
        List<String> commands = new ArrayList<String>();
        commands.addAll(ShellFile.findByName("/etc/sysconfig/network-scripts/","ifcfg-en","if_in"));
        commands.add(ShellFile.replaceContent("'s/IPADDR="+oldIp+"/IPADDR="+newIp+"/g","$if_in"));
        return commands;
    }

    public static List<String> hosts(List<String> hosts){
        List<String> commands = new ArrayList<String>();
        return commands;
    }
}
